#!/bin/bash
# usage: send-upc-draft-release-mail draft_number draft_date
#
# 'send-upc-draft-release-mail' will send the UPC Specification
# Draft release notice out to various relevant mailing lists.
#   draft_number	The release number of the draft (1, 2, 3 ...)
#   draft_date		The exact date string after the line beginning
#			with the string 'Date:' in the release note.
#
# Example: send-upc-draft-release-mail 1 'August 25, 2012'
#
# ('draft_date' is used as a cross-check to prevent accidental
# execution of this script, which would in turn send unsolicited
# email messages far and wide.)
#
function error {
  msg="$1"
  echo "$msg" 1>&2
  exit 2
}

spec_version="1.3"
addr_list="upc@hermes.gwu.edu upc-users@lbl.gov upc-spec@hermes.gwu.edu
 upc-spec-dev@googlegroups.com upc-spec-wg@googlegroups.com"
pgm="$0"
[ $# -eq 2 ] || error "${pgm}: expects two arguments"
draft_version="$1"
draft_date="$2"
spec_notice_version=`echo ${spec_version} | sed -e 's/\./-/'`
rls_subj="UPC Specification ${spec_version} Draft ${draft_version}"
rls_note="upc-spec-${spec_notice_version}-draft-${draft_version}-notice.txt"
[ -f "$rls_note" ] || error "not found: $rls_note"
grep -sq "^Date: *${draft_date}$" "$rls_note" >/dev/null 2>&1 \
  || error "Draft release date mis-match: $draft_date"
# The TO environment variable can be used to over-ride
# the default address list for testing purposes.
TO=${TO:-$addr_list}
type -p mutt >/dev/null 2>&1 || error "command not found: mutt"
for addr in $TO; do
  echo "mutt -s'$rls_subj' '$addr'"
  mutt -s"$rls_subj" "$addr" < $rls_note 
done
